extends Button
class_name CustomClickButton

signal click_only_signal
signal set_button_key_signal(key)

@onready var label: Label = $Label
@onready var base_shader = preload("res://scenes/control_setup/CustomClickButton.gdshader")
var button_text_bak

# 铵钮按下闪烁
var blinking : bool = false:
	set(value):
		blinking = value
		if value:
			material.set_shader_parameter("active", true)
			click_only_signal.emit()
		else:
			material.set_shader_parameter("active", false)
			if button_text_bak != null:
				label.text = button_text_bak

func _ready() -> void:
	label.text = self.name
	var shader_material = ShaderMaterial.new()
	shader_material.shader = base_shader
	material = shader_material

func _on_pressed() -> void:
	if not blinking:
		button_text_bak = label.text
		label.text = ""
	blinking = !blinking

func _input(event: InputEvent) -> void:
	if blinking and event is InputEventKey:
		var lower_key = OS.get_keycode_string(event.keycode).to_lower()
		#print(lower_key)
		if lower_key == "escape":
			blinking = false
		else:
			set_button_key(lower_key)
			blinking = false

func set_button_key(key: String) -> void:
	button_text_bak = null
	label.text = key
	set_button_key_signal.emit(key)
